// RegionTop10Driver.java
package com.taxi.analysis.regionTop10;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class RegionTop10Driver {

    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.err.println("Usage: RegionTop10Driver <input path> <output path>");
            System.exit(-1);
        }

        Configuration conf = new Configuration();

        // 第一个Job：统计各地区接单数
        Job job1 = Job.getInstance(conf, "region order count");
        job1.setJarByClass(RegionTop10Driver.class);
        job1.setMapperClass(RegionOrderMapper.class);
        job1.setCombinerClass(RegionOrderReducer.class);
        job1.setReducerClass(RegionOrderReducer.class);
        job1.setOutputKeyClass(Text.class);
        job1.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job1, new Path(args[0]));
        Path tempPath = new Path(args[1] + "_temp");
        FileOutputFormat.setOutputPath(job1, tempPath);

        // 等待第一个Job完成
        if (!job1.waitForCompletion(true)) {
            System.exit(1);
        }

        // 第二个Job：获取TOP10
        Job job2 = Job.getInstance(conf, "top 10 regions");
        job2.setJarByClass(RegionTop10Driver.class);
        job2.setMapperClass(Top10Mapper.class);
        job2.setReducerClass(Top10Reducer.class);
        job2.setMapOutputKeyClass(NullWritable.class);
        job2.setMapOutputValueClass(Text.class);
        job2.setOutputKeyClass(Text.class);
        job2.setOutputValueClass(Text.class);
        job2.setNumReduceTasks(1); // 确保只有一个Reducer来处理TOP10

        FileInputFormat.addInputPath(job2, tempPath);
        FileOutputFormat.setOutputPath(job2, new Path(args[1]));

        System.exit(job2.waitForCompletion(true) ? 0 : 1);
    }
}
